<?php
/**
 * 权限管理模块.
 * User: 1874
 * Date: 2019/5/8
 * Time: 15:05
 */

namespace app\adminplatform\controller;

use app\adminplatform\model\AuthGroup;
use app\adminplatform\model\AuthRule;
use app\adminplatform\model\Log;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Auth extends Initial {
    /*
     * 权限列表
     */
    public function authRuleList(){
        $map = [];
        //方法名称
        if(input('name')) $map[] = ['ar_name','like','%'.input('name').'%'];
        //方法URL
        if(input('method')) $map[] = ['ar_method','=',input('method')];
        //状态
        if(input('status') != null) $map[] = ['ar_status','=',input('status')];
        //如果接收到pid
        $pid = input('pid');
        if($pid) {
            $map[] = ['ar_pid','=', $pid];
        }else{
            $pid = 0;
            $map[] = ["ar_pid", "=", 0];
        }
        //查询数据
        $info = AuthRule::authRuleList($map);
        //递归查找该权限所有的上级
        $parent = array_reverse(AuthRule::getParentAuthRule($pid));
        //管理员权限
        $rules = [
            'Auth/authRuleAdd', 'Auth/authRuleEdit','Auth/authRuleDel', 'Auth/authRuleDelAll', 'Auth/authRuleRedis'
        ];
        $check = AuthGroup::checkMethodArray(Session::get('userData'), $rules);

        View::assign('info',$info);
        View::assign('check',$check);
        View::assign('parent', $parent); //递归查询所有上级
        return View::fetch('authRuleList');
    }
    /*
     * 权限redis缓存更新
     */
    public function authRuleRedis(){
        //判断请求类型
        if(Request::isPost()){
            //权限存入redis并读取
            $info = AuthRule::setAuthRule();
            if(count($info)){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'), '更新权限缓存');

                $data = ['info'=>'更新成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，更新失败了','status'=>0];
            }
        }else{
            $data = ['info'=>'请求类型错误','status'=>0];
        }
        return json($data);
    }
    /*
     * 权限新增
     */
    public function authRuleAdd(){
        //判断请求类型
        if(Request::isPost()){
            $request = input();
            $map['ar_name']  = $request['name'];
            $map['ar_pid']  = $request['pid'];
            $map['ar_method']  = $request['method'];
            //$map['ar_beforeAfter']  = $request['beforeAfter'];
            $map['ar_status']  = $request['status'];
            //检查信息是否完整
            checkField($map);
            //新增信息
            $info = AuthRule::create($map);
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'新增权限【'.$map['ar_name'].'】');

                $data = ['info'=>'新增成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，新增失败了','status'=>0];
            }
            return json($data);
        }
        //如果接收到父id
        if(input('pid')) {
            $data['pid'] = input('pid');
            $data['parent'] = AuthRule::authRuleInfo(["ar_id"=>$data['pid']])->ar_name;
        }else{
            $data['pid'] = 0;
            $data['parent'] = '顶级';
        }
        View::assign('data',$data);
        return View::fetch('authRuleAdd');
    }
    /*
     * 权限编辑
     */
    public function authRuleEdit(){
        $id = input('id');
        //判断请求类型
        if(Request::isPost()){
            $request = input();
            $map['ar_id']  = $request['id'];
            $map['ar_name']  = $request['name'];
            $map['ar_method']  = $request['method'];
            //$map['ar_beforeAfter']  = $request['beforeAfter'];
            $map['ar_status']  = $request['status'];
            //检查字段是否为空
            checkField($map);
            //修改信息
            $info = AuthRule::update($map, ["ar_id"=>$map["ar_id"]]);
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'编辑权限【'.$map['ar_name'].'】');

                $data = ['info'=>'编辑成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，信息没有改变','status'=>0];
            }
            return json($data);
        }
        //权限信息
        $info = AuthRule::authRuleInfo(["ar_id"=>$id]);
        //上级信息
        $info['parent'] = AuthRule::authRuleInfo(["ar_id"=>$info->ar_pid])->ar_name;
        if(!$info['parent']) $info['parent'] = '顶级';

        View::assign('info',$info);
        return View::fetch('authRuleEdit');
    }
    /*
     * 权限删除
     */
    public function authRuleDel(){
        //判断请求类型
        if(!Request::isPost()){
            $data = ['info'=>'请求类型不是这个哦，亲','status'=>0];
        }else{
            $arId = input('id');
            //查询是否还有子权限
            $children = AuthRule::authRuleInfo(["ar_pid"=>$arId], 1);
            if($children > 0){
                $data = ['info'=>'权限所属下还有下级权限，无法删除','status'=>0];
                return json($data);
            }
            $authRule = AuthRule::authRuleInfo(["ar_id"=>$arId]);
            //删除信息
            $info = $authRule->delete();
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'删除权限【'.$authRule->ar_name.'】');

                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，删除失败了','status'=>0];
            }
        }

        return json($data);
    }
    /*
     * 权限批量删除
     */
    public function authRuleDelAll(){
        //判断请求类型
        if(!Request::isPost()){
            $data = ['info'=>'请求类型不是这个哦，亲','status'=>0];
        }else{
            $ids = input('ids');
            //权限下方是否还有所属下级权限
            foreach ($ids as $value){
                //是否有下级权限
                $child = AuthRule::authRuleInfo(["ar_pid"=>$value], 1);
                if ($child) {
                    $name = AuthRule::authRuleInfo(["ar_id"=>$value])->ar_name;
                    $data = ['info' => '【' . $name . '】还有下级权限，无法删除', 'status' => 0];
                    return json($data);
                }
            }
            //删除信息
            $info = AuthRule::where("ar_id", "in", $ids)->delete();
            if($info){
                //写入日志
                Log::logAdd(Session::get('userData.ad_id'),'删除权限'.$info.'条');

                $data = ['info'=>'删除成功','status'=>1];
            }else{
                $data = ['info'=>'不好意思，删除失败了','status'=>0];
            }
        }

        return json($data);
    }
}